package com.financing.platform.dao.impl;


import com.financing.platform.common.Pager;
import com.financing.platform.dao.IProjectDao;
import com.financing.platform.dto.*;
import com.financing.platform.util.SqlName;
import com.financing.platform.vo.ProductVO;
import com.financing.platform.vo.ProjectSendConfirmEmailVO;
import com.financing.platform.vo.ProjectSubVO;
import com.financing.platform.vo.ProjectVO;

import org.springframework.stereotype.Repository;

import java.sql.SQLException;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("projectDaoImpl")
public class ProjectDaoImpl extends BaseDaoImpl 
		implements IProjectDao { 

	@SuppressWarnings("unchecked")
	public List<ProjectDTO> getProjectList(ProjectDTO projectDTO) {
		try {
			return (List<ProjectDTO>)this.sqlMapClient.queryForList(SqlName.queryAllProject, projectDTO);
		} catch (SQLException e) {
			log.error("Query all Project error :",e);
			return Collections.emptyList();
		}
	}

	public ProjectDTO searchProjectByIdOrCode(ProjectDTO projectDTO) {
		try {
			return (ProjectDTO)this.sqlMapClient.queryForObject(SqlName.queryAllProject, projectDTO);
		} catch (SQLException e) {
			log.error("Query all Project error :",e);
			return projectDTO;
		}
	}

	public int updateProject(ProjectDTO projectDTO) throws SQLException {
		return this.sqlMapClient.update(SqlName.updateProject, projectDTO);
	}
	public int updateProjectFull(ProjectDTO projectDTO) throws SQLException {
		return this.sqlMapClient.update(SqlName.updateProjectFull, projectDTO);
	}
	

	public int addProject(ProjectDTO projectDTO) throws SQLException {
		
		return (Integer) this.sqlMapClient.insert(SqlName.insertProject, projectDTO);
	}

	public int autoUpdateSaleStatus() {
		try {
			this.sqlMapClient.update(SqlName.autoUpdateSaleStatusTo03);
			return this.sqlMapClient.update(SqlName.autoUpdateSaleStatusTo05);
		} catch (SQLException e) {
			log.error("auto update sale status error !",e);
			return 0;
		}
	}

	public int updateAllSubscription(ProjectDTO projectDTO) throws SQLException {
		return this.sqlMapClient.update(SqlName.updateAllSubscription,projectDTO);
	}

	public int deleteProject(int projectId) throws SQLException {
		return this.sqlMapClient.delete(SqlName.deleteProject, projectId);
	}

	public int insertProductAttachment(ProductAttachmentDTO productAttachmentDTO)
			throws SQLException {
		return (Integer) this.sqlMapClient.insert(SqlName.addProductAccachement,productAttachmentDTO);
	}
	
	public int deleteProductAttachment(int productId) throws SQLException{
		return this.sqlMapClient.delete(SqlName.deleteProductAttachment,productId);
	}

	@SuppressWarnings("unchecked")
	public List<ProjectVO> projectListForPublish(ProjectDTO projectDTO)
			throws SQLException {
		return (List<ProjectVO>)this.sqlMapClient.queryForList(SqlName.productForPublishList, projectDTO);
	}

	@Override
	public int projectListForPublishCount(ProjectDTO projectDTO)
			throws SQLException {
		return (Integer) this.sqlMapClient.queryForObject(SqlName.productForPublishListCount, projectDTO);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ProductAttachmentDTO> queryProductAttachments(Integer pId)
			throws SQLException {
		return (List<ProductAttachmentDTO>) this.sqlMapClient.queryForList(SqlName.queryProductAccachement, pId);
	}

	@Override
	public ProductAttachmentDTO queryProductAttachmentsForId(Integer Id) throws SQLException {
		return (ProductAttachmentDTO)this.sqlMapClient.queryForObject("project.queryProductAttachmentsForId", Id);
	}

	@Override
	public List<ProjectSendConfirmEmailVO> queryProjectInfoByIds(Integer[] array) throws SQLException {
		return (List<ProjectSendConfirmEmailVO>) this.sqlMapClient.queryForList(SqlName.queryProjectInfoByIds, array);
	}

	@Override
	public String queryUUID() throws SQLException {
		return (String)this.sqlMapClient.queryForObject(SqlName.queryUUID);
	}

	@Override
	public int addProjectExpand(ProjectExpandDTO projectExpandDTO)
			throws SQLException {
		return  (Integer) this.sqlMapClient.insert(SqlName.insertProjectExpand, projectExpandDTO);
	}

	@Override
	public int updateProjectExpand(ProjectExpandDTO projectExpandDTO)
			throws SQLException {
		return this.sqlMapClient.update(SqlName.updateProjectExpand, projectExpandDTO);
	}

	@Override
	public int selectProjectExpand(Integer pId) throws SQLException {
		return (Integer) this.sqlMapClient.queryForObject(SqlName.selectProjectExpand,pId);
	}

	@Override
	public List<ProjectSendConfirmEmailVO> queryOutProductInfoByIds(
			Integer[] array) throws SQLException {
		return this.sqlMapClient.queryForList(SqlName.queryOutProductInfoByIds,array);
	}

	@Override
	public int addForegroundProductLog(ProjectDTO projectDTO) throws SQLException {
		return (Integer) this.sqlMapClient.insert(SqlName.addForegroundProductLog,projectDTO);
	}	
		/**
	 * ��ѯ���Ʒ��ص��������
	 * @throws SQLException 
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<FpProjectRabate> getProductProfitRebateList(Integer productId) throws SQLException {
		
		
		return this.sqlMapClient.queryForList(SqlName.getProductProfitRebateLists, productId);
	}
		@Override
	public int addProjectFlow(ProjectSubVO projectSubVO) throws SQLException {
		return (Integer) this.sqlMapClient.insert(SqlName.addProjectFlow,projectSubVO);
	}

	@Override
	public ProjectDTO searchProjectById(ProjectDTO projectDTO) throws SQLException {
		Integer id = projectDTO.getId();
		return (ProjectDTO)this.sqlMapClient.queryForObject(SqlName.searchProjectBypId, id);
	}	
	
	/**
	 * 根据产品名称获取产品详情
	 * @return
	 * @throws SQLException 
	 */
	public ProjectDTO searchProjectByName(ProjectDTO projectDTO) throws SQLException{
		List<ProjectDTO> queryForObject = this.sqlMapClient.queryForList(SqlName.searchProjectByName, projectDTO);
		 return (queryForObject==null||queryForObject.size()==0)?new ProjectDTO():queryForObject.get(0);
	}

	@Override
	public ProductDetailPageDTO getProductDetailPage(Integer productId) throws SQLException{
		return (ProductDetailPageDTO)this.sqlMapClient.queryForObject("project.getProductDetailPage", productId);
	}

	@Override
	public ProductAnnualRevenueDTO queryAnnualRevenue(int id,String sort)  throws SQLException{
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("id", id);
		params.put("sort", sort);
		return (ProductAnnualRevenueDTO) this.sqlMapClient.queryForObject("project.queryAnnualRevenue", params);
	}

	@Override
	public Map<String, Object> queryProductBuyNumberAndAmount(Integer productId) throws SQLException{
		return (Map<String,Object>)this.sqlMapClient.queryForObject("project.queryProductBuyNumberAndAmount", productId);
	}

	@Override
	public int countProductListForQD(Map<String, Object> queryParams,Pager pager) throws SQLException {

		Map<String,Object> params = new HashMap<String,Object>();
		params.put("queryParams", queryParams);
		params.put("pager", pager);
		return (Integer) this.sqlMapClient.queryForObject("project.countProductListForQD",params);
	}

	@Override
	public List<ProductVO> queryProductListForQD(Map<String, Object> queryParams,Pager pager) throws SQLException {
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("queryParams", queryParams);
		params.put("pager", pager);
		return this.sqlMapClient.queryForList("project.queryProductListForQD", params);
	}

	@Override
	public List<Map<String, Object>> queryInvestOwner(String investOwnerName) throws SQLException{
		return  this.sqlMapClient.queryForList("project.queryInvestOwner", investOwnerName);
	}

    @Override
    public ProductVO queryProductQDDetail(Integer pId) throws SQLException{
        return (ProductVO)this.sqlMapClient.queryForObject("project.queryProductQDDetail", pId);
    }

	@Override
	public Map<String, Object>  queryPnameByCode(String pCode) throws SQLException {
		return (Map<String, Object>) this.sqlMapClient.queryForObject(SqlName.queryPnameByCode, pCode);
	}
}
